package com.mybatis.bing.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatis.bing.entity.User;
import com.mybatis.bing.mapper.UserMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@SpringBootTest
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Test
    //查询所有用户
    @RequestMapping("/user")
    public List findAll(User user) {
        List<User> users = userMapper.selectList ( null );
        System.out.println ( users );
        return users;
    }


    //添加用户
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public String addUser(@RequestBody User user) {
        try {
            int result = this.userMapper.insert ( user );
            if (result > 0) { // empty string
                System.out.println ( "result = " + result );
                return "用户添加成功!";
            }
        } catch (Exception e) {
            System.out.println ( "该用户已经存在请重新添加用户!" );
            return "该用户已经存在请重新添加用户!";
        }

        return "用户添加失败";
    }

    //修改用户
    @RequestMapping(value = "/updateById", method = RequestMethod.GET)
    public String updateById(String username, String password, String email, String phone, Integer id, User user) {
        try {
            user.setUsername ( username );//更新的字段
            user.setPassword ( password );//更新的字段
            user.setEmail ( email );//更新的字段
            user.setPhone ( phone );//更新的字段
            //更新的条件
            QueryWrapper<User> wrapper = new QueryWrapper<> ();
            wrapper.eq ( "id", id );
            //执行更新操作
            int result = this.userMapper.update ( user, wrapper );
            if (result > 0) { // empty string
                System.out.println ( "result = " + result );
                return "修改用户成功!";
            }
        } catch (Exception e) {
            System.out.println ( "该用户已经存在请重新修改用户!" );
            return "该用户已经存在请重新修改用户!";
        }
        return "用户修改失败";
    }


    //删除用户
    @RequestMapping(value = "/deleteUserById", method = RequestMethod.GET)
    public String deleteUserById(Long id, User user) {
        try {
            //将实体对象进行包装，包装为操作条件
            QueryWrapper<User> wrapper = new QueryWrapper<> ( user );
            wrapper.eq ( "id", id );
            int result = this.userMapper.delete ( wrapper );
            if (result > 0) { // empty string
                System.out.println ( "result = " + result );
                return "删除用户成功!";
            }

        } catch (Exception e) {
            System.out.println ( "用户不存在!" );
            return "用户不存在!";
        }
        return "用户不存在!";
    }

    @GetMapping("/tt")
    public String tt() {
        return "tt";
    }

    //分页
    @RequestMapping(value = "/queryUserForPage", method = RequestMethod.GET)
    public String queryUserForPage(Integer current, Integer size) {
        IPage<User> userPage = new Page<> ( current, size );//参数一是当前页，参数二是每页个数
        userPage = userMapper.selectPage ( userPage, null );
        List<User> list = userPage.getRecords ();
        for (User user : list) {
            System.out.println ( user );
            return String.valueOf ( user );
        }
        return "已经滑到底了，不要再滑了";
    }


}
